<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd"
                    v-hasRole="['dormManagement2']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" size="mini" @click="handleDelete2"
                    v-hasRole="['dormManagement2']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" size="mini" @click="handleExport"
                    v-hasRole="['dormManagement']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <el-table v-loading="loading" :data="recordList" stripe border>
            <el-table-column label="宿舍" prop="dormName" :show-overflow-tooltip="true" width="80" sortable />
            <el-table-column label="时间" prop="time" :show-overflow-tooltip="true" width="80" sortable />
            <el-table-column label="用水量(吨)" prop="waterConsum" :show-overflow-tooltip="true" width="120" sortable />
            <el-table-column label="水费(元)" prop="waterCharge" width="100" sortable />
            <el-table-column label="用电量(度)" align="center" prop="electricityConsum" width="120" sortable />
            <el-table-column label="电费(元)" align="center" prop="electricityCharge" width="100" sortable />
            <el-table-column label="缴费" prop="isPay" width="90" sortable>
                <template #default="scope">
                    <el-tag class="ml-2" v-if="scope.row.isPay == 1">
                        已缴费
                    </el-tag>
                    <el-tag class="ml-2" type="danger" v-if="scope.row.isPay == 0">
                        未缴费
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="addTime" width="160" sortable />
            <el-table-column label="创建者" align="center" prop="addName" width="120" sortable />
            <el-table-column label="最后修改时间" align="center" prop="modifyTime" width="160" sortable />
            <el-table-column label="修改者" align="center" prop="modifyName" width="120" sortable />
            <!-- <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="70">
                <template #default="scope">
                    <el-button size="mini" v-if="scope.row.isPay === 0" type="text" icon="Promotion"
                        @click="startPay(scope.row)" v-hasRole="['student']">缴费</el-button>
                </template>
            </el-table-column> -->
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>

<script setup name="dormManagementFareInfo">
import { ElNotification } from 'element-plus'
import { getWaterElectricityInfoApi } from '@/api/dormMangement'
import { exportExcel } from '@/utils/excelExport'
const { proxy } = getCurrentInstance();
const showSearch = ref(true)
const recordList = ref([]);
const loading = ref(true);
const total = ref(0);

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10
    }
});

const { queryParams } = toRefs(data);

/** 查询水电费信息列表 */
const getList = () => {
    loading.value = true;
    getWaterElectricityInfoApi(queryParams.value).then(res => {
        if (res.code == 200) {
            recordList.value = res.data.vo;
            total.value = res.data.total;
            loading.value = false;
        } else {
            recordList.value = [];
            loading.value = false;
        }
    }).catch(err => {
        recordList.value = [];
        total.value = 0;
        loading.value = false;
    })
}
getList();
/** 导出按钮操作 */
function handleExport() {
    let arr = []
    recordList.value.forEach(item => {
        let out = {}
        out.time = item.time;
        out.dormId = item.dormId;
        out.waterConsum = item.waterConsum;
        out.waterCharge = item.waterCharge;
        out.electricityConsum = item.electricityConsum;
        out.electricityCharge = item.electricityCharge;
        if (item.isPay == 1) {
            out.isPay = '已缴费'
        } else {
            out.isPay = '未缴费'
        }
        out.addTime = item.addTime;
        out.addName = item.addName;
        out.modifyTime = item.modifyTime;
        out.modifyName = item.modifyName
        arr.push(out)
    })
    const titleArr = ['时间', '宿舍', '用水量(吨)', '水费(元)', '用电量(度)', '电费(元)', '缴费', '创建时间', '创建者', '最后修改时间', '最后修改者']  //表头中文名
    exportExcel(arr, '水电费信息', titleArr, 'Sheet1')
}
</script>
